package com.mx.zb.mng.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;

import com.mx.zb.mng.utils.ex.InputFormatException;

/**
 * 
 * @ClassName: StringToDateConverter
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author 张博
 * @date 2017年9月8日 上午11:02:23 
 *
 */
public class StringToDateConverter implements Converter<String, java.util.Date> {
	
	private final Logger log = LoggerFactory.getLogger(this.getClass()); 

	private DateFormat df;

	public StringToDateConverter(String pattern) {
		df = new SimpleDateFormat(pattern);
	}

	@Override
	public Date convert(String source) {
		if (StringUtils.hasText(source)) {
			try {
				return df.parse(source);
			} catch (ParseException e) {
				throw new InputFormatException("日期的格式不正确。", e);
			}
		}
		return null;
	}
	
	

}
